/**
 * 
 */
package com.depot.auth.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.depot.auth.dao.AppRoleDao;
import com.depot.domains.AppRole;
import com.opensymphony.xwork2.ModelDriven;
import com.soar.BaseActionSupport;
import com.soar.context.Environment;

/**
 * 
 * @author Gravey
 * 
 */
public class AppRoleAction extends BaseActionSupport implements ModelDriven<AppRole>
{
	private static final long serialVersionUID = 9219835850744524732L;

	public AppRole domain = new AppRole();

	public HashMap<String, String> approle = new HashMap<String, String>();

	public List<HashMap<String, String>> approles = new ArrayList<HashMap<String, String>>();

	public List<HashMap<String, String>> appmenu = new ArrayList<HashMap<String, String>>();

	/**
	 * 应用角色列表
	 * 
	 * @param env
	 * @param domain
	 * @param action
	 * @return
	 * @throws Exception
	 */
	public String execute(Environment env) throws Exception
	{
		this.sysPageList = AppRoleDao.select(env, domain, this);
		return "app_role_list";
	}

	/**
	 * 应用角色更新
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public String doSave(Environment env) throws Exception
	{
		String result = AppRoleDao.doSave(env, domain);
		if (result.equals("1"))
		{
			this.setMessage("操作处理成功！", env, true);
		}
		else if (result.equals("0"))
		{
			this.setMessage("操作处理失败，请重试！", env, false);
		}
		else if (result.equals("2"))
		{
			this.setMessage("名称重复，操作处理失败，请修改后再试！", env, false);
		}
		return null;
	}

	/**
	 * 应用角色新增
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public String doNew(Environment env) throws Exception
	{
		return "app_role_edit";
	}

	/**
	 * 应用角色删除
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public String doDelete(Environment env) throws Exception
	{
		String result = AppRoleDao.doDelete(env, domain);
		if ("1".equals(result))
		{
			this.setMessage("删除成功！");
		}
		else
		{
			this.setMessage("删除失败！");
		}
		return this.execute(env);
	}

	/**
	 * 应用角色单行查询
	 * 
	 * @param env
	 * @param domain
	 * @return
	 * @throws Exception
	 */
	public String doShow(Environment env) throws Exception
	{
		this.approle = AppRoleDao.doShow(env, domain);
		return this.doNew(env);
	}

	public String roleMenu(Environment env) throws Exception
	{
		// this.approle = AppRoleDao.doShow(env, domain);
		this.approles = AppRoleDao.doShowPl(env, domain);
		this.appmenu = AppRoleDao.selectMenu(env, domain);
		this.sysPageList = AppRoleDao.select(env, domain);
		return "role_menu";
	}

	public String saveRoleMenu(Environment env) throws Exception
	{
		String res = AppRoleDao.saveRoleMenu(env, domain);
		if ("1".equals(res))
		{
			this.setMessage("操作成功", env, true);
		}
		else
		{
			this.setMessage("操作失败，请重试！", env, false);
		}
		return null;
	}

	public String getRoles(Environment env) throws Exception
	{
		this.domain.setIds(this.domain.getId().split(","));
		this.sysPageList = AppRoleDao.getRoles(env, domain);
		String ids = "";
		for (HashMap<String, String> map : sysPageList)
		{
			ids += map.get("MENU_ID") + ",";
		}
		env.getResponse().getWriter().print(ids);
		return null;
	}

	public String chooseRole(Environment env) throws Exception
	{
		this.sysPageList = AppRoleDao.select(env, domain);
		return "app_role_list_ajax";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.opensymphony.xwork2.ModelDriven#getModel()
	 */

	public AppRole getModel()
	{
		return this.domain;
	}
}